package ai.complex_path_finding;

import search.Search;
import hexgame.GameState;
import hexgame.HexMove;
import hexgame.HexPlayer;
import hexgame.Player;

public class AStarAIPlayer extends Player {

	GameState gameState;
	HexMove firstMove = null;
	HexMove latestMove = null;
	
	int numberOfMoves = 0;
	long computationTime = 0;
	
	
	public AStarAIPlayer(GameState gameState) {
		this.gameState = gameState;
	}

	@Override
	public boolean isConnected() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public HexMove getMove() {
		HexMove move = null;
		long startTime = System.nanoTime();
		if(firstMove == null){
			this.makeFirstMove();
			move = firstMove;
		} else {
			HexMove[] path = Search.leastTurnsToEdge(latestMove, gameState, true);
			move = new HexMove(path[0].getX(), path[0].getY(), HexPlayer.RED_LOCAL);
		}
		latestMove = move;
		
		long timeForMove = System.nanoTime() - startTime;
		numberOfMoves++;
		System.out.println(numberOfMoves  + ":  " + timeForMove + "\t Average:  " + timeForMove/numberOfMoves);
		computationTime += timeForMove;
		
		return move;
	}

	@Override
	public void gameOver(boolean didWin) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendErrorMessage(String message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showMove(HexMove move) {
		// TODO Auto-generated method stub
		
	}
	
	private void makeFirstMove() {
		if(gameState.getHexState(0, gameState.getBoardSize()/2) == HexPlayer.NONE) {
			firstMove = new HexMove(0, gameState.getBoardSize()/2 , HexPlayer.RED_LOCAL);
		} else {
			firstMove = new HexMove(0, gameState.getBoardSize()/2 + 1, HexPlayer.RED_LOCAL);
		}
	}

}
